#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <boost/filesystem.hpp>
#include <boost/timer.hpp>
#include <boost/program_options.hpp>
#include <bitset>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>

#include "Detecting.h"
#include "image_cutting_together.h"
#include "line_detect.h"
#include "detect_loop.h"
#include "pretreatment.h"


using namespace cv;
using namespace std;

void sharpenImage0(const cv::Mat &image, cv::Mat &result)
{
    //为输出图像分配内存
    result.create(image.size(),image.type());
    
   /*滤波核为拉普拉斯核3x3：
                            0 -1 0
                           -1 5 -1
                            0 -1 0   
   */
   for(int j= 1; j<image.rows-1; ++j)
   {
      const uchar *previous = image.ptr<const uchar>(j-1);
      const uchar *current = image.ptr<const uchar>(j);
       const uchar *next = image.ptr<const uchar>(j+1);
         uchar *output = result.ptr<uchar>(j);
         for(int i= 1; i<image.cols-1; ++i)
         {
             *output++ = cv::saturate_cast<uchar>(5*current[i]-previous[i]-next[i]-current[i-1]-current[i+1]);  //saturate_cast<uchar>()保证结果在uchar范围内
         }
     }
     result.row(0).setTo(cv::Scalar(0));
     result.row(result.rows-1).setTo(cv::Scalar(0));
     result.col(0).setTo(cv::Scalar(0));
     result.col(result.cols-1).setTo(cv::Scalar(0));
 }


void sharpenImage1(const cv::Mat &image, cv::Mat &result)
{
    //创建并初始化滤波模板
    cv::Mat kernel(3,3,CV_32F,cv::Scalar(0));
    kernel.at<float>(1,1) = 5.0;
    kernel.at<float>(0,1) = -1.0;
    kernel.at<float>(1,0) = -1.0;
    kernel.at<float>(1,2) = -1.0;
    kernel.at<float>(2,1) = -1.0;
    result.create(image.size(),image.type());    
    //对图像进行滤波
    cv::filter2D(image,result,image.depth(),kernel);
}

int main()
{
    //////yolo3//////
    // YOLOv3::Detecting detector;
    // vector<YOLOv3::DetectResult> result;
    // namedWindow("src");
    // char filename[100];
    ///////////////

    // Mat sharp = imread("/home/g/yolo3/yolo_crack/src/crack_detect/pic/test.png",0);
    // Mat sharp_res;
    // sharpenImage0(sharp , sharp_res);
    // imwrite("res.jpg",sharp_res);

    // Mat sharp = imread("/home/g/yolo3/yolo_crack/src/crack_detect/pic/test.png",0);
    // Mat sharp_res;
    // equalizeHist(sharp, sharp_res);
    // imwrite("res.jpg",sharp_res);

    // Mat gam = imread("/home/g/yolo3/yolo_crack/src/crack_detect/pic/test.png");
    // Mat gam_res;
    // pretreatment pre;
    // pre.gamma(gam, gam_res, 0.7);
    // //gamma_correction(gam, gam_res, 0.7);
    // imwrite("res.jpg",gam_res);

    
    // Mat src = imread("/home/g/yolo3/yolo_crack/src/crack_detect/pic/text.jpg");
    // Mat src2 = imread("/home/g/yolo3/yolo_crack/src/crack_detect/pic/text.jpg");
    // Mat src3;
    // image_cutter_together toge;
    // toge.image_together_vertical(src,src2,src3);
    // imwrite("src.jpg",src3);
    // Mat srcmmmm = imread("/home/g/yolo3/yolo_crack/src/crack_detect/pic/test.png");
    // std::vector<cv::Mat> cutcut;



    // cutcut = toge.image_cutter(srcmmmm ,1000 ,2000);
    // for(int i = 0 ; i<cutcut.size() ; i++)
    // {
    //    string  name1 =  std::to_string(i) + ".jpg";
    //    imwrite(name1,cutcut[i]);
    // }





    // Mat res;
    // line_detecter lie;
    // std::vector<cv::Vec4i> mm = lie.detect_lines(srcmmmm);
    // float a,b;
    // lie.lines_to_line(mm,a,b);
    // cout<<a<<" @@@@@@@@@@@@@@ "<<b<<endl;
    // image_cutter_together cut;
    // Mat src1 = srcmmmm.clone();
    // cutcut = cut.image_cutter(src1, int(a) , int(b));
    //  for(int i = 0 ; i<cutcut.size() ; i++)
    // {
    //    string  name1 =  std::to_string(i) + ".jpg";
    //    imwrite(name1,cutcut[i]);
    // }
    

    //yolo yolo yolo
    Mat mod2;
    //Mat mod = imread("/home/g/yolo3/yolo_crack/src/crack_detect/pic/text.jpg");
    Mat mod = imread("/home/g/yolo3/yolo_crack/src/crack_detect/pic/test.png");
    cout<<mod.cols<<" "<<mod.rows<<endl;
    detect_loop detecterit("/home/g/yolo3/yolo_crack/src/crack_detect/config/");
    detecterit.yolo_v3_introduction_weights();
    //detecterit.yolo_crack_detect_imitate(mod,mod2);
    detecterit.yolo_crack_detect_on_parameters(mod,mod2,1000,2000);
    imwrite("resmmmm.jpg",mod2);


    //   YOLOv3::Detecting detector;
    //   vector<YOLOv3::DetectResult> result;
    //   vector<YOLOv3::DetectResult> result2;
    //   Mat mod = imread("/home/g/yolo3/yolo_crack/src/crack_detect/pic/1.jpg");
    //   Mat mod2;
    //   detector.Detect(mod,result);
    //   for(int i = 0 ;i<result.size(); i++)
    //   {
    //       if(result[i].mName == "crack")
    //       {
    //           result2.push_back(result[i]);
    //       }
    //   }
    //   cout<<result.size()<<endl;
    //   detector.DrawResult(mod,result2);
    //   imwrite("res.jpg",mod);






    //imwrite("qianye.jpg",res);
	
    //////yolo3///////
    // for(int i = 1; i<3; i++)
    // {
    //     sprintf(filename,"/home/g/yolo3/yolo_crack/src/crack_detect/pic/text.jpg",i);
    //     cout<<filename<<endl;
    //     Mat src = imread(filename);
    //     detector.Detect(src,result);
    //     detector.DrawResult(src,result);
    //     cout<<result[0].mName<<result[0].mConfidence<<result[0].mTop<<result[0].mBottom<<result[0].mLeft<<result[0].mRight<<endl;
    //     imwrite("src.jpg",src);
    //     waitKey(30);
    // }
    return 0;
}

//https://blog.csdn.net/qinchang1/article/details/86749196  clone
